package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 *
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

   /* 先判断id是都为空
 *    为空直接添加
 *    不为空即为修改。在多张表情况下，修改即为先删除，再增加*/

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;



    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private DistrictInfoService districtInfoService;


    @Override
    public void pageApartmentList(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
       apartmentInfoMapper.pageApartmentSelect(page,queryVo);
    }

    @Override
    public void removeByApartmentId(Long id) {
        //先判断公寓是否有房间，若有房间，
        //利用异常处理流派，直接抛出错误
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomInfo> eq = roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(eq);

        if(count>0){
            throw new  LeaseException(ResultCodeEnum.DELETE_ERROR.getMessage(),ResultCodeEnum.DELETE_ERROR.getCode());
        }

        //根据id删除公寓
        removeById(id);

        //删除配套公寓中间表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //删除公寓标签中间表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //删除杂费信息中间表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //删除图片表
        //既要删除对应的apartmentSubmitVo的id，也要删除对应的type
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);




    }

    @Override
    public ApartmentDetailVo apartmentDetailVoListById(Long id) {

        //根据公寓id获取公寓信息
        ApartmentInfo apartmentInfo = getById(id);

        //根据公寓id获取图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectByApamentId(id,ItemType.APARTMENT);

        //根据公寓id获取公寓配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);

        //根据公寓id获取公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentId(id);

        //根据公寓id获取获取公寓杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectByApartmentId(id);

        //把查询结果放在一起
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        return apartmentDetailVo;
    }

    @Override
    public void ApartmentsaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //判断id是否为空
        Long id = apartmentSubmitVo.getId();
        //如果不为空,删除信息
        if (id != null){
            //删除配套公寓中间表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除公寓标签中间表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //删除杂费信息中间表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //删除图片表
                //既要删除对应的apartmentSubmitVo的id，也要删除对应的type
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //添加信息
            //添加公寓表信息
        saveOrUpdate(apartmentSubmitVo);


            //添加配套公寓信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().
                        apartmentId(apartmentSubmitVo.getId()).
                        facilityId(facilityInfoId).
                        build();
                apartmentFacilities.add(build);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

            //添加标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().
                        apartmentId(apartmentSubmitVo.getId()).
                        labelId(labelId).
                        build();
                apartmentLabels.add(build);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

            //添加杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentLabel build = ApartmentLabel.builder().
                        apartmentId(apartmentSubmitVo.getId()).
                        labelId(feeValueId).
                        build();
                apartmentLabels.add(build);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

            //添加图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }



    }
}




